Part 13.3 - Machine Learning Python Programming Training

Convolutional Neural Network (CNN)

Image Data

When the computer see the image, it sees pixel values.
image.png

image.png

CNN Architecture

image.png

  1. Input layer (4 dimensional vector)
  2. Convolution layer– Finding local pattern
  3. Pooling layer
  4. Flatten
  5. Fully-connected layers – Finding global pattern
  6. Output layer

1. Input layer (4 dimensional vector)

image.png

2. Convolutions

Convolution – the mathematical combination of two functions to produce a third function.
image.png

image.png

3. Pooling

Pooling with filters of 2 X 2

4. Flatten Layer - Architecture

The flattening section of CNN architecture will flatten a pooled layer to one column so that the data will be passed through ANN to get processed further. The neuron of flatten layer detects a certain feature like nose, mouth,,,
In this CNN architecture, pooling layer of (4,4,20) will be flattened to flatten layer of (4x4x20=320), which means that it has 320 image features.

image.png

5. Fully-connected layer

The fully-connected layer section of CNN has three layers.

6. Output Layer

The output layer section outputs an N (e.g., 10) dimensional vector where N is the number of classes that the program has to choose from.

For example,

How Image features are progressed throughout CNN architecture

image.png

Famous CNN Architecture competition winner

Convolution Python Programm

Below is CNN acchitecture that the data scientist builds for the workshop.

## Train the model model_cnn.fit(X_tain, y_train, batch_size=128, epoch=10, validation_split=0.2) ## Predict with the model cnn_preds = model_cnn.predict(X_test) ## Evaluate the trained model model_cnn.evaluate(X_test, y_test)

CNN Implementation

Recurrent Neural Network

Introduction – recurrent neural network model to use sequential information.

Why RNN?

Basic RNN Structure and Algorithms

image.png

image.png

RNN unit - LSTM (Long Short-Term Memory Unit)

image.png

Text notation. # of inputs = # of outputs image.png

Sentimental Analysis (PV signal) : x = text, y = 0/1 or 1 to 5 image.png

Music generation \ Picture Description: x= vector, y = text image.png

Machine translation : x = text in English, y = text in French image.png

Natural Language Processing (NLP) using RNN

Introduction of NLP – An area of artificial intelligence on an interaction between computer and human natural language. NLP can program computers to process and analyze natural language data.

Simple RNN architecture using NLP

image.png

Natural Language Processing (NLP) procedures

  1. Import data and preparation
  2. Tokenizing – representing each word to numeric integer number : “the” to 50
  3. Padding – fixing all the records to the same length
  4. Embedding – representing word(numeric number) to vectors of numbers
    5o to [ 0.418, 0.24968, -0.41242, 0.1217, 0.34527, -0.044457, -0.49688, -0.17862, -0.00066023,,,,, ]
  5. Training with RNN models

1. Import Data and Preparation

2. Tokenization

3. Padding

4. Embedding

## import gensim model from gensim.models import Word2Vec## creat NLP model using gensim for embeding model_NLP = Word2Vec(line, min_count=1) print(model_NLP.wv.vocab) print(line) print(model_NLP['i']) print(model_NLP['i'].shape)

5. Train with RNN model

tokenizer = Tokenizer() tokenizer.fit_on_texts(X_train_rnn) X_input = tokenizer.texts_to_sequence(X_train_rnn)

RNN Implementation

Who will learn faster Python programming

Why experience programmers can learn other or new language faster?

Transfer Learning

Machine Learning method where a pre-trained model is reused as a starting point of the model development for another/similar task. image.png

For Image Data

For Language Data

Keras Transfer Learning

How model learns in Image Recognition

Pre-trained Model : VGG

Transfer Learning Implementation

  1. Select pre-trained model
  2. Import pre-trained model
  3. Determine which parameters/layers of the pre-trained model should be trained
  4. Add more layers of the imported pre-trained model
  5. Set up the output layers
  6. Prepare data features (input data) for pre-trained model
  7. Compile pre-trained and added layers
  8. Train the new model

Why Transfer Learning?

image.png

VGG16 model in Keras

vgg16_model = Sequential()

vgg16_model.add(Conv2D(64, (3, 3), input_shape=input_shape, padding='same', activation='relu'))
vgg16_model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
vgg16_model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

vgg16_model.add(Conv2D(128, (3, 3), activation='relu', padding='same'))
vgg16_model.add(Conv2D(128, (3, 3), activation='relu', padding='same'))
vgg16_model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

vgg16_model.add(Conv2D(256, (3, 3), activation='relu', padding='same'))
vgg16_model.add(Conv2D(256, (3, 3), activation='relu', padding='same'))
vgg16_model.add(Conv2D(256, (3, 3), activation='relu', padding='same'))
vgg16_model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

vgg16_model.add(Conv2D(512, (3, 3), activation='relu', padding='same'))
vgg16_model.add(Conv2D(512, (3, 3), activation='relu', padding='same'))
vgg16_model.add(Conv2D(512, (3, 3), activation='relu', padding='same'))
vgg16_model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

vgg16_model.add(Conv2D(512, (3, 3), activation='relu', padding='same'))
vgg16_model.add(Conv2D(512, (3, 3), activation='relu', padding='same'))
vgg16_model.add(Conv2D(512, (3, 3), activation='relu', padding='same'))
vgg16_model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))

vgg16_model.add(Flatten())
vgg16_model.add(Dense(4096, activation='relu'))
vgg16_model.add(Dense(4096, activation='relu'))
vgg16_model.add(Dense(1000, activation='softmax'))

## Train the model model_vgg_tf.fit(X_tain_tf, y_train_tf, batch_size=128, epoch=10, validation_split=0.2) ## Predict with the model vgg_tf_preds = model_vgg_tf.predict(X_test_tf) ## Evaluate the trained model model_vgg_tf.evaluate(X_test_tf, y_test_tf)

BERT Model

BERT Training

BERT uses two training strategies

  1. Masked ML - Before feeding into BERT, 15% of the words are replaced with mask(empty) token and BERT model is trained image.png
  1. Next Sentence Prediction
    • Input – pairs of sentences
    • Three embedding – token, sentence and positional
    • Labels - IsNext / NotNext image.png

Use pre-trained Mask BERT model to fill the word in blank

Predict the word in a situation that it is missing.
"The first death in the US [blank] reported on February 29"
What could be included in here?

Import pre-trained BERT Library

Create BERT Tokenizer and feature preparation

Load pre-trained model

Predict with Pre-trained BERT model

Use pre-trained BERT model (next sentence prediction)

Predict where the second sentence is the next sentence.

Imporrt BERT Next Sentence Prediction library

Prpeare input data (features)

Import pre-trained BERT model for next sentence prediction

Predict if it is the next sentence

CONCLUSION

CONTACT INFORMATION

Your comments and questions are valued and encouraged. Please contact at:
Kevin Lee
AVP of Machine Learning and AI Consultant
Kevin.lee@genpact.com
Tweet: @HelloKevinLee
LinkedIn: www.linkedin.com/in/HelloKevinLee/

Questions

image.png